# -*- coding: utf-8 -*-
# __init__.py create by kumiler
# on 2022/6/24 17:25
# desc
from datetime import timedelta
from utils.operators.rich_sql_sensor import RichSqlSensor
# from jms.dm.dm_waybill_customer_complaint.complaint_0530.dm_waybill_customer_complaint_agg_new_dt import jms_dm__dm_waybill_customer_complaint_agg_new_dt0530
# from jms.dm.dm_waybill_customer_complaint.complaint_0530.dm_waybill_customer_complaint_detail_new_dt import jms_dm__dm_waybill_customer_complaint_detail_new_dt0530
from time import time

# 客诉率明细
label_detail = f"dwd_waybill_customer_complaint_detail_new_dt_05_{int(time())}"
timeout = timedelta(minutes=30).seconds
doris_jms_dwd__dwd_waybill_customer_complaint_detail_new_dt0530 = RichSqlSensor(
    task_id='doris_jms_dwd__dwd_waybill_customer_complaint_detail_new_dt0530',
    pool='broker_load_pool',
    email=['shenjiaming@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='new_doris',
    pre_sql=f"""
             TRUNCATE TABLE jms_dwd.dwd_waybill_customer_complaint_detail_new_dt PARTITION (p{{{{ execution_date | cst_ds_nodash }}}});
             LOAD LABEL jms_dwd.{label_detail} (
                 DATA INFILE(
"hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_customer_complaint_detail_new_dt/dt={{{{ execution_date | cst_ds }}}}/*"
)
                 INTO TABLE dwd_waybill_customer_complaint_detail_new_dt
                 FORMAT AS 'PARQUET'
             )
             WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dwd WHERE LABEL = '{label_detail}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dwd WHERE LABEL = '{label_detail}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

# doris_jms_dwd__dwd_waybill_customer_complaint_detail_new_dt0530 << jms_dm__dm_waybill_customer_complaint_detail_new_dt0530


label_dt = f"dws_waybill_customer_complaint_agg_new_dt_05_{int(time())}"
doris_jms_dws__dws_waybill_customer_complaint_agg_new_dt0530= RichSqlSensor(
    task_id='doris_jms_dws__dws_waybill_customer_complaint_agg_new_dt0530',
    pool='broker_load_pool',
    email=['shenjiaming@jtexpress.com','yl_bigdata@yl-scm.com'],
    conn_id='new_doris',
    pre_sql=f"""
             TRUNCATE TABLE jms_dws.dws_waybill_customer_complaint_agg_new_dt PARTITION (p{{{{ execution_date | cst_ds_nodash }}}});
             LOAD LABEL jms_dws.{label_dt} (
                 DATA INFILE("hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_customer_complaint_agg_new_dt/dt={{{{ execution_date | cst_ds }}}}/*")
                 INTO TABLE dws_waybill_customer_complaint_agg_new_dt
                 FORMAT AS 'PARQUET'
             )
             WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dws WHERE LABEL = '{label_dt}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dws WHERE LABEL = '{label_dt}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

# doris_jms_dws__dws_waybill_customer_complaint_agg_new_dt0530<<jms_dm__dm_waybill_customer_complaint_agg_new_dt0530